<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\sign_in;


use app\common\model\sign_in\SignInReward;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * SignInReward逻辑
 * Class SignInRewardLogic
 * @package app\adminapi\logic
 */
class SignInRewardLogic extends BaseLogic
{



    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/17 14:07
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            $datas = json_decode($params['details'], true);
            foreach ($datas as $k => $v) {
                SignInReward::where('id', $v['id'])->update([
                    'award' => $v['award'],
                ]);
            }
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 获取签到配置
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/01/17 14:07
     */
    public static function config($params): array
    {
        return SignInReward::where('app_id', $params['app_id'])->field(['id', 'name', 'award'])->order('sort asc')->select()->toArray();
    }


    /**
     * 复制平台签到
     *
     * @param $platformId
     * @return bool|int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function copyAppSign($appId)
    {
        $list = SignInReward::where('app_id', '=', 1)->order('id', 'asc')->select();
        if ($list->isEmpty()) {
            return false;
        }
        $insertData = [];
        foreach ($list as $item) {
            array_push($insertData, [
                'name'     => $item['name'] ?? '',
                'award'   => $item['award'] ?? '',
                'sort'     => $item['sort'] ?? '',
                'app_id' => $appId,
            ]);
        }

        return SignInReward::insertAll($insertData);
    }
}
